package cdu.xjj.controller;


import cdu.xjj.anno.Log;
import cdu.xjj.pojo.Dept;
import cdu.xjj.pojo.Result;
import cdu.xjj.service.DeptService;
import cdu.xjj.service.impl.DeptServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    //private static final Logger log= LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;

    //查询全部部门数据
    @Log
    @GetMapping
    public Result list(){

        //System.out.println("查询全部部门数据");
         log.info("查询全部部门数据");
         List<Dept> deptList = deptService.findAll();
         return Result.success(deptList);
    }

    //删除部门
    @Log
    @DeleteMapping
    public Result delete(Integer id){
        //System.out.println("根据ID删除部门："+id);
        log.info("根据ID删除部门：{}",id);
        deptService.deteleById(id);
        return Result.success();
    }

    //添加部门
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        //System.out.println("新增部门："+ dept);
        log.info("新增部门：{}",dept);
        deptService.add(dept);
        return Result.success();

    }

    //查询部门
    @Log
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        //System.out.println("根据ID查询部门："+ id);
        log.info("根据ID查询部门：{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    //修改部门信息
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        //System.out.println("修改部门："+ dept);
        log.info("修改部门：{}", dept);
        deptService.update(dept);
        return Result.success();
    }
}
